package com.test.translateanimation;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {

	private Button btn_move = null, btn_hello_word = null;
	private TextView tv_hello_word = null;
	private boolean isDown = false;

	private TranslateAnimation translateAnimationDown, translateAnimationUp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initAnimation();
		initView();

	}

	private void initAnimation() {
		translateAnimationDown = new TranslateAnimation(0, 0, 0, 500);
		translateAnimationDown.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_interpolator));
		translateAnimationDown.setFillAfter(true);
		translateAnimationDown.setFillEnabled(true);
		translateAnimationDown.setDuration(2000);
		translateAnimationDown.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				LayoutParams params = (LayoutParams) btn_hello_word.getLayoutParams();
				params.setMargins(0, 500, 0, 0);
				btn_hello_word.setLayoutParams(params);
				btn_hello_word.clearAnimation();
				isDown = true;
			}
		});

		translateAnimationUp = new TranslateAnimation(0, 0, 0, -500);
		translateAnimationUp.setInterpolator(this, android.R.anim.accelerate_interpolator);
		translateAnimationUp.setFillAfter(true);
		translateAnimationUp.setFillEnabled(true);
		translateAnimationUp.setDuration(2000);
		translateAnimationUp.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				LayoutParams params = (LayoutParams) btn_hello_word.getLayoutParams();
				params.setMargins(0, 0, 0, 0);
				btn_hello_word.setLayoutParams(params);
				btn_hello_word.clearAnimation();
				isDown = false;
			}
		});
	}

	private void initView() {
		btn_move = (Button) findViewById(R.id.btn_move);
		btn_hello_word = (Button) findViewById(R.id.btn_hello_word);
		tv_hello_word = (TextView) findViewById(R.id.tv_hello_word);

		btn_move.setOnClickListener(this);
		btn_hello_word.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_move:
			if (isDown) {
				// tv_hello_word.startAnimation(translateAnimationUp);
				btn_hello_word.startAnimation(translateAnimationUp);
			} else {
				// tv_hello_word.startAnimation(translateAnimationDown);
				btn_hello_word.startAnimation(translateAnimationDown);
			}
			break;
		case R.id.btn_hello_word:
			Toast.makeText(this, "这是点击彪子", Toast.LENGTH_LONG).show();
			break;

		default:
			break;
		}
	}
}
